package com.slg.android.manyanime.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.adapter.CommentAdapter;
import com.slg.android.manyanime.bean.Comment;
import com.slg.android.manyanime.bean.User;
import com.slg.android.manyanime.utils.DataStore;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


public class LastCommentFragment extends Fragment implements PullToRefreshBase.OnRefreshListener<ListView> {
    @ViewInject(R.id.comment_last_comment)
    private PullToRefreshListView pullToRefreshListView;
    private ListView listView;
    private String json;
    private List<Comment> data;
    private String total;
    private int haveNext;
    private String lastId = "";
    private CommentAdapter adapter;
    private String mid;
    private String uid = "";
    private String type;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        json = arguments.getString("data");
        Log.d("data", "last  " + json);
        mid = arguments.getString("mid");
        type = arguments.getString("type");
        loadData(json, false);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_last_comment, container, false);
        ViewUtils.inject(this, view);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        pullToRefreshListView.setOnRefreshListener(this);
        listView = pullToRefreshListView.getRefreshableView();
        adapter = new CommentAdapter(getActivity(), data);
        listView.setAdapter(adapter);
        return view;
    }

    private void loadData(String jsonString, boolean loadMore) {
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            total = jsonObject.getString("total");
            haveNext = jsonObject.getInt("have_next");
            lastId = jsonObject.getString("last_id");
            if (loadMore) {
                data.addAll(Comment.parseData(jsonString));
            } else {
                data = Comment.parseData(jsonString);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        if (refreshView.isRefreshing()) {
            HttpUtils httpUtils = new HttpUtils();
            User user = DataStore.getDataStore().getUser();
            if (user != null) {
                uid = user.getId();
            }
            httpUtils.send(HttpRequest.HttpMethod.GET,
                    "http://manhua.api.61.com/?&pf=1&android_version=16&app_version=512&app_channel=%E7%99%BE%E5%BA%A6&m=Index&a=commentList&uid=" + uid + "&last_id=" + lastId + "&mid=" + mid + "&module="+type+"&order=new&login_uid=" + uid
                    , new RequestCallBack<String>() {
                        @Override
                        public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                            if (haveNext != 1) {
                                pullToRefreshListView.onRefreshComplete();
                            } else {
                                loadData(stringResponseInfo.result, true);
                                adapter.notifyDataSetChanged();
                                pullToRefreshListView.onRefreshComplete();
                            }
                        }

                        @Override
                        public void onFailure(HttpException e, String s) {
                            Toast.makeText(getActivity(), R.string.no_network, Toast.LENGTH_SHORT).show();
                            pullToRefreshListView.onRefreshComplete();
                        }
                    });
        }
    }
}
